package com.mayflygeek.mayflyask.framework.orm.exception;

import java.io.Serializable;

import com.mayflygeek.mayflyask.framework.core.Idable;

/**
 * @author 虫虫
 * @team 蜉蝣极客
 * @link www.mayflygeek.com
 * @Emial chongchong@mayflygeek.com
 * 
 */
public class PersistenceException extends RuntimeException {
	private static final long serialVersionUID = -15816759316099323L;

	public PersistenceException() {
		super();
	}

	public PersistenceException(Class<?> type, Serializable id, String action, String msg, Throwable throwable) {
		super(String.format("%s类型%s, id = %s: %s", action, type.getName(), String.valueOf(id), msg), throwable);
	}

	public PersistenceException(Object entity, String action, String msg, Throwable throwable) {
		this(entity.getClass(), id(entity), action, msg, throwable);
	}

	private static Serializable id(Object entity) {
		if (entity instanceof Idable) {
			return ((Idable<?>) entity).getId();
		}

		// TODO
		return "?";
	}
}
